package com.nnxy.worktostudy.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnxy.worktostudy.system.dto.response.ApplyListItem;
import com.nnxy.worktostudy.system.entity.Apply;
import com.nnxy.worktostudy.system.entity.Job;
import com.nnxy.worktostudy.system.entity.Project;
import com.nnxy.worktostudy.system.entity.Student;
import com.nnxy.worktostudy.system.service.ApplyService;
import com.nnxy.worktostudy.system.dto.request.ApplyRequestDto;
import com.nnxy.worktostudy.system.service.JobService;
import com.nnxy.worktostudy.system.service.ProjectService;
import com.nnxy.worktostudy.system.service.StudentService;
import com.nnxy.worktostudy.utils.ResponseFormat;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luye
 * @since 2021-10-14
 */
@RestController
@RequestMapping("/system/apply")
@CrossOrigin
public class ApplyController {

    @Resource
    private ApplyService applyService;

    /**
     * 审核功能，修改申请记录status
     * @param obj 前端数据
     * @return ResponseFormat
     */
    @RequestMapping("/audit")
    public ResponseFormat audit(@RequestBody ApplyRequestDto obj) {
        Apply apply = applyService.getById(obj.getId());

        if (apply == null) return ResponseFormat.failed().setMessage("数据库中没有这条数据！");

        apply.setStatus(obj.getStatus());
        applyService.updateById(apply);

        return ResponseFormat.succeed();
    }

    /**
     * 获取列表 分页
     * @param obj 前端数据
     * @return ResponseFormat
     */
    @RequestMapping("/getPagList")
    public ResponseFormat getPagList(@RequestBody ApplyRequestDto obj) {
        Map<String,Object> result = applyService.page(new QueryWrapper<>(), obj.getPageSize(), obj.getCurrentPage());
        return ResponseFormat.succeed().setData(result);
    }

    /**
     * 搜索功能 分页
     * @param obj 前端数据
     * @return ResponseFormat
     */
    @RequestMapping("/select")
    public ResponseFormat select(@RequestBody ApplyRequestDto obj) {
        QueryWrapper<Apply> queryWrapper = new QueryWrapper<>();

        if (obj.getId() != null) {
            queryWrapper.eq("student_id", obj.getId());
        }
        else {
            switch (obj.getStatus()) {
                case 0:
                    break;
                case 1:
                    queryWrapper.eq("status", 0).or().eq("status", 3);
                    break;
                case 2:
                    queryWrapper.eq("status", 1).or().eq("status", 2);
                    break;
                default:
                    return ResponseFormat.failed().setMessage("搜索失败，数值超出范围！");
            }
        }

        Map<String,Object> result = applyService.page(queryWrapper, obj.getPageSize(), obj.getCurrentPage());

        return ResponseFormat.succeed().setData(result);
    }

}

